package org.mymail.config;

import com.x8Peppers.test.targetNS.MailConfigDocument;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import org.apache.xmlbeans.XmlException;
import org.mymail.mail.MailConstants;

// Referenced classes of package org.mymail.config:
//            MailAuthenticator

public class GmailMailAuthenticator extends Authenticator implements
		MailAuthenticator {

	public PasswordAuthentication getPasswordAuthentication() {
		HashMap<String, String> authInfo = getAuthInfo();
		String uname = (String) authInfo.get("uname");
		String password = (String) authInfo.get("password");
		return new PasswordAuthentication(uname, password);
	}

	private HashMap<String, String> getAuthInfo() {
		HashMap<String, String> authInfo = new HashMap<String, String>();
		try {
			MailConfigDocument configDoc = com.x8Peppers.test.targetNS.MailConfigDocument.Factory
					.parse(new File(
							MailConstants.configXMLLoc));
			com.x8Peppers.test.targetNS.MailConfigDocument.MailConfig config = configDoc
					.getMailConfig();
			authInfo.put("uname", config.getEmail());
			authInfo.put("password", config.getPassword());
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return authInfo;
	}
}